#define DBC_CHECK_PRECONDITION

using System;
using System.Xml;

using DesignByContract;

namespace Outcoding.GraphGenerator.Entities.XmlBuilders {
    public class IndustryXmlBuilder : IXmlBuilder {

        private Industry mResultObject = null;

        public Industry ResultObject {
            get {
                return mResultObject;
            }
        }

        public IndustryXmlBuilder(XmlNode industryXmlNode) {
            Check.Require(industryXmlNode != null, "Xml source node cannot be null");
            this.BuildObject(industryXmlNode);
        }

        private void BuildObject(XmlNode industryXmlNode) {
            mResultObject = new Industry();
            try {
                mResultObject.Name = ParseNameAttribute(industryXmlNode);
                if (industryXmlNode.Attributes["introductory-slide"] != null)
                    mResultObject.IsIntroductory = ParseIsIntroductoryAttribute(industryXmlNode);
            } catch (DesignByContract.PreconditionException preExc) {
                //log error
                mResultObject = null;
                throw;
            }
        }

        private string ParseNameAttribute(XmlNode industryXmlNode) {
            Check.Require(industryXmlNode.Attributes["name"] != null, "name attribute not present");
            return industryXmlNode.Attributes["name"].Value;
        }

        private bool ParseIsIntroductoryAttribute(XmlNode industryXmlNode) {
            Check.Require(industryXmlNode.Attributes["introductory-slide"] != null, "introductory-slide attribute not present");
            return Boolean.Parse(industryXmlNode.Attributes["introductory-slide"].Value);
        }
    }
}
